<?php
	class LoginController
	{
		// Membervaribles
		private $_loginHandler = null;
		
		public function __construct(\LoginHandler $loginHandler)
		{
			$this->_loginHandler = $loginHandler;
		}
			
		public function DoControl()
		{
			$xhtml = "";
									
			$view = new LoginView();
						
			// Check if the user is logged in, if he is not...
			if($this->_loginHandler->IsLoggedIn() == false)
			{
				
				// Check if the user pressed Login.
				if ($view->TriedToLogin() == true)
				{
					$this->_loginHandler->DoLogin($view->GetUsername(), $view->GetPassword());
					
					if ($this->_loginHandler->IsLoggedIn() == true)
					{
						if ($view->RememberLogin() == true)
						{
							$view->SetCookie();
						}
						
						$view->SetUsernameInSession();
						$view->LoginMessage();
					}
					else
					{
						$view->LoginErrorMessage();
					}	
				}
			}
			else
			{
				// Check if the user pressed Logout.
				if ($view->TriedToLogout() == true)
				{
					$this->_loginHandler->DoLogout();
					$view->UnSetCookie();
					$view->LogoutMessage();
				}
			}
			
			// Check wether the user is Logged in or out and print the form.
			if ($this->_loginHandler->IsLoggedIn() == false)
			{
				$xhtml .= $view->DoLoginBox();
			}
			else
			{
				$xhtml .= $view->DoLogoutButton();
			}
			
			return $xhtml;
		}

	}